﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadCRM.Entities;

namespace LeadCRM.Controllers
{
    public class AccountController : Controller
    {
        LeadCRM.API.UserMgr userMgr = new API.UserMgr();

        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Register(LeadCRM.Models.Account.Register model)
        {
            try
            {                
                User user = new User();
                user.Username = model.Username;
                
                user.Email = model.Email;
                user.IsApproved = true;
                
                user.IsLockedOut = false;
                user.LastLoginDate = null;
                user.LastPasswordChangedDate = null;
                user.Password = model.Password;

                var RegisterStatus = WebSecurity.Register(model.Username, model.Password, model.Email, true,
                    model.FirstName, model.LastName);

                ViewBag.Message = "Registeration Failed";

                switch (RegisterStatus )
                {
                    case System.Web.Security.MembershipCreateStatus.DuplicateEmail:
                        break;
                    case System.Web.Security.MembershipCreateStatus.DuplicateProviderUserKey:
                        break;
                    case System.Web.Security.MembershipCreateStatus.DuplicateUserName:
                        break;
                    case System.Web.Security.MembershipCreateStatus.InvalidAnswer:
                        break;
                    case System.Web.Security.MembershipCreateStatus.InvalidEmail:
                        break;
                    case System.Web.Security.MembershipCreateStatus.InvalidPassword:
                        break;
                    case System.Web.Security.MembershipCreateStatus.InvalidProviderUserKey:
                        break;
                    case System.Web.Security.MembershipCreateStatus.InvalidQuestion:
                        break;
                    case System.Web.Security.MembershipCreateStatus.InvalidUserName:
                        break;
                    case System.Web.Security.MembershipCreateStatus.ProviderError:
                        break;
                    case System.Web.Security.MembershipCreateStatus.Success:
                        ViewBag.Message = "Added new user to the system";
                        break;
                    case System.Web.Security.MembershipCreateStatus.UserRejected:
                        break;
                    default:
                        break;
                }
                //LeadCRMContext db = new LeadCRMContext();
                //db.User.Add(user);
                //db.SaveChanges();
                

                return View();
            }

            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {
                string error = "";
                foreach (var eve in e.EntityValidationErrors)
                {
                    error +=string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        error += string.Format("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                ViewBag.Message = error;
                return View();
            }
        }

        //User Management 
        public ActionResult ManageUsers()
        {
            ViewBag.Users = userMgr.GetAllUser(20,0);
            return View();
        }

        

        

        

        
    }
}
